<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>${empty department.id ? '添加新部门' : '编辑部门'}</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/style.css">
</head>
<body>
<div class="container">
    <jsp:include page="/header.jsp" />
    <h2>${empty department.id ? '添加新部门' : '编辑部门'}</h2>
    <form action="${pageContext.request.contextPath}/DepartmentServlet" method="post">
        <input type="hidden" name="action" value="${empty department.id ? 'add' : 'update'}">
        <c:if test="${not empty department.id}">
            <input type="hidden" name="id" value="${department.id}">
        </c:if>
        <p>
            <label for="name">部门名称:</label>
            <input type="text" id="name" name="name" value="<c:out value='${department.name}'/>" required>
        </p>
        <p>
            <label for="managerId">部门负责人:</label>
            <select id="managerId" name="managerId">
                <option value="0">-- 无负责人 --</option> <%-- 0 表示不选择或清空负责人 --%>
                <c:forEach var="emp" items="${employeeList}"> <%-- employeeList 由 Servlet 传入 --%>
                    <option value="${emp.id}" ${department.managerId == emp.id ? 'selected' : ''}>
                        <c:out value="${emp.name}"/> (ID: ${emp.id})
                    </option>
                </c:forEach>
            </select>
        </p>
        <p><input type="submit" value="${empty department.id ? '添加' : '更新'}"></p>
    </form>
    <p><a href="${pageContext.request.contextPath}/DepartmentServlet?action=list">返回列表</a></p>
</div>
</body>
</html>
